﻿using System;
using System.Runtime.CompilerServices;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

public static class UIExtension
{
    public static void SetActive(this Canvas canvas, bool value)
    {
        canvas.gameObject.SetActive(value);
    }

    public static void SetListener(this Button button, UnityAction call)
    {
        button.onClick.SetListener(call);
    }

    public static void SetListener(this Button.ButtonClickedEvent evt, UnityAction call)
    {
        evt.RemoveAllListeners();
        evt.AddListener(call);
    }

    public static void SetListener(this Toggle toggle, UnityAction<bool> call)
    {
        toggle.onValueChanged.SetListener(call);
    }

    public static void SetListener(this Toggle.ToggleEvent evt, UnityAction<bool> call)
    {
        evt.RemoveAllListeners();
        evt.AddListener(call);
    }

    public static void SetText(this Button button, string text)
    {
        button.transform.Find("Text").GetComponent<Text>().text = text;
    }

    public static void SetText(this Toggle toggle, string text)
    {
        toggle.transform.Find("Text").GetComponent<Text>().text = text;
    }

    public static void SetTextAndListener(this Button button, string text, UnityAction call)
    {
        button.SetText(text);
        button.onClick.SetListener(call);
    }

    public static void SetTextAndListener(this Toggle toggle, string text, UnityAction<bool> call)
    {
        toggle.SetText(text);
        toggle.onValueChanged.SetListener(call);
    }
}

